package com.airplane.hotdiscuss.ui;

import com.airplane.hotdiscuss.R;
import com.airplane.hotdiscuss.network.HttpControllerImpl;
import com.airplane.hotdiscuss.network.IHttpController;
import com.airplane.hotdiscuss.util.Constants;
import com.airplane.hotdiscuss.util.MyLogger;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebView extends Activity {
    private static final MyLogger logger = MyLogger.getLogger(MyWebView.class.getSimpleName());

    private static final int CACHE_MAX_SIZE = 2097152; // 2*1024*1024

    private String mWebUrl;
    private WebView mWebView;
    private WebSettings mWebSettings;
    private IHttpController mHttpController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        logger.d("onCreate() --> Enter");
        super.onCreate(savedInstanceState);

        mHttpController = HttpControllerImpl.getInstance(this);

        setContentView(R.layout.my_web_view);
        mWebView = (WebView) findViewById(R.id.web_view);

        Intent intent = getIntent();
        mWebUrl = intent.getExtras().getString(Constants.INTENT_KEY_WEBURL);
        logger.d("Web url is : " + mWebUrl);

        mWebSettings = mWebView.getSettings();
        mWebSettings.setDefaultTextEncodingName("AUTOSELECT");
        mWebSettings.setSupportZoom(true);
        mWebSettings.setBuiltInZoomControls(true);
        //mWebSettings.setUseWideViewPort(true);
        //mWebSettings.setJavaScriptEnabled(true);
        //mWebSettings.setAppCacheEnabled(true);
        //mWebSettings.setAppCacheMaxSize(CACHE_MAX_SIZE);

        /*mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }
        });*/

        if (!mHttpController.isDataLinkOpened()) {
            mHttpController.openDataLink();
        } else {
            mWebView.loadUrl(mWebUrl);
        }

        logger.d("onCreate() --> Exit");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mWebUrl = intent.getExtras().getString(Constants.INTENT_KEY_WEBURL);
        logger.d("onNewIntent(), get web url : " + mWebUrl);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                    return false;
                }
                break;
            default:
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        logger.d("onDestroy() --> Enter");
        super.onDestroy();

        if (mWebView != null) {
            mWebView.clearCache(true);
            mWebView.clearHistory();
        }
        logger.d("onDestroy() --> Exit");
    }
}
